﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Dynamic;
using M;
using Mysoft.MyCM.Scm;

namespace Mysoft.MyCM.Business.Scm
{
	/// <summary>
	/// 项目业务处理
	/// </summary>
	public class ProjectBLL : BaseBLL
	{
		/// <summary>
		/// 获取客户包含的项目信息
		/// </summary>
		/// <param name="customId">TFS客户Id</param>
		/// <returns></returns>
		public List<Project> GetCustomProjects(Guid customId)
		{
			MValidator.CheckForEmptyGUID("TFS客户Id", customId);
			using (var client = ScmWebApiClientManager.Manager.GetClient())
			{
				var result = client.GetSync<ApiResult<List<Project>>>(string.Format("g/Custom/GetProjectByCustom/{0}", customId));
				CheckResult(result);
				return result.Result;
			}
		}
		/// <summary>
		///  获取客户包含的项目信息 包含附加拼接信息
		/// </summary>
		/// <param name="customId"></param>
		/// <returns></returns>
		public List<Project2> GetCustomProjects2(Guid customId)
		{
			MValidator.CheckForEmptyGUID("TFS客户Id", customId);
			using (var client = ScmWebApiClientManager.Manager.GetClient())
			{
				var result = client.GetSync<ApiResult<List<Project2>>>(string.Format("g/Custom/GetProjectByCustom/{0}", customId));
				CheckResult(result);
				// 获取 Custom2
				CustomBLL cstBLL = new CustomBLL();
				Custom2 cst = cstBLL.GetCustom2(customId);
				result.Result.ForEach(pj2 =>
				{
					pj2.CustomTfsUrl = cst.TfsUrl;
					pj2.ProjectCollectionBasePath = cst.ProjectCollectionBasePath;
					pj2.DevTfsProjectPath = string.Format("{0}/{1}/源代码/", cst.ProjectCollectionBasePath, pj2.ProjectName);
					pj2.DevDBFullName = pj2.DevDBInstance == null ? string.Empty : string.Format("{0}\\{1}", pj2.DevDBInstance.Server.IpAddress, pj2.DevDBInstance.DBInstanceName);
					pj2.TestWebFullName = pj2.WebServer == null ? string.Empty : string.Format("{0}（{1}）", pj2.WebServer.IpAddress, pj2.WebServer.ServerName);
					pj2.TestDBFullName = pj2.TestDBInstance == null ? string.Empty : string.Format("{0}\\{1}", pj2.TestDBInstance.Server.IpAddress, pj2.TestDBInstance.DBInstanceName);
				});
				return result.Result;
			}
		}
	}
}
